package kau.dao;

import java.util.ArrayList;
import java.util.HashMap;

import org.apache.ibatis.session.SqlSession;

import kau.common.DBUtil;
import kau.exception.DuplicationException;
import kau.exception.RecordNotFoundException;
import kau.vo.CurriPlan;

public class CurriPlanDAOImpl implements CurriPlanDAOIF {

	DBUtil db = null;
	private static CurriPlanDAOImpl instance;
	
	private CurriPlanDAOImpl() {
		db = DBUtil.getInstance();
	}
	public static CurriPlanDAOImpl getInstance(){
		if( instance == null) instance = new CurriPlanDAOImpl();
		return instance;
	}
	
	public boolean isExist(int courseId,int week) {
		SqlSession session = db.getSession();
		boolean flag = false;
		String result = null;
		HashMap<String, Integer> info = new HashMap<>();
		info.put("courseId", courseId);
		info.put("week", week);
		try{
			result = session.selectOne("dbConfig.CurriPlanMapper.isExist",info);
		}finally{
			session.close();
		}
		if(result!= null) flag= true;
		
		return flag;
	}
	
	@Override
	public void insert(CurriPlan curriPlan) throws DuplicationException {
		
		SqlSession session = db.getSession();
		try{
			session.insert("dbConfig.CurriPlanMapper.insert", curriPlan);
			session.commit();
		}finally{
			session.close();
		}

	}

	@Override
	public void update(CurriPlan curriPlan)
			throws RecordNotFoundException {
		
		SqlSession session = db.getSession();
		try{
			session.update("dbConfig.CurriPlanMapper.update", curriPlan);
			session.commit();
		}finally{
			session.close();
		}

	}

	@Override
	public void delete(int courseId,int week)
			throws RecordNotFoundException {
		HashMap<String, Integer> info = new HashMap<>();
		info.put("courseId", courseId);
		info.put("week", week);
		SqlSession session = db.getSession();
		try{
			session.delete("dbConfig.CurriPlanMapper.delete", info);
			session.commit();
		}finally{
			session.close();
		}
	}

	
	@Override
	public CurriPlan getCurriPlan(int courseId,int week)
			throws RecordNotFoundException {
		HashMap<String, Integer> info = new HashMap<>();
		info.put("courseId", courseId);
		info.put("week", week);
		SqlSession session = db.getSession();
		CurriPlan curriPlan=null;
		try{
			curriPlan = session.selectOne("dbConfig.CurriPlanMapper.getCurriPlan", info);
		}finally{
			session.close();
		}
		return curriPlan;
	}
	
	@Override
	public ArrayList<CurriPlan> getAllCurriPlans(int courseId) {
		ArrayList<CurriPlan> curriPlanList = new ArrayList<CurriPlan>();
		SqlSession session = db.getSession();
		try{
			curriPlanList = (ArrayList) session.selectList("dbConfig.CurriPlanMapper.getAllCurriPlans",courseId);
		} finally{
			session.close();
		}
		return curriPlanList;
	}
	@Override
	public void allDelete(int courseId) throws RecordNotFoundException {
		// TODO Auto-generated method stub
	
		
		SqlSession session = db.getSession();
		try{
			session.delete("dbConfig.CurriPlanMapper.allDelete", courseId);
			session.commit();
		}finally{
			session.close();
		}
	}
	

}
